<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Signup extends CI_Controller {
	
	function __construct()
	{
		parent::__construct();
	}
	
	/**
	 * Default Signup page
	 *
	 * @access	public
	 * @output	signup.tpl
	 */
	public function index()
	{		
		$this->load->helper(array('recaptchalib'));
	
		$data['title'] = "Create an Account";
		$data['captcha'] = recaptcha_get_html($this->config->item('recap_public'));
		$data['errors'] = $this->session->flashdata('errors');
		$data['refill'] = $this->session->flashdata('refill');
			
		$this->parser->parse("signup/signup.tpl", $data);
	}
	
	/**
	 * Register
	 *
	 * @access		public
	 * @redirect	Success: 		/
	 *				Unsuccessful: 	/signup
	 */
	public function register()
	{			
		$this->load->helper(array('form','url','recaptchalib'));
		$this->load->library(array('form_validation'));
		$this->load->model(array('User_model'));
		$this->config->load('config');
			
		if (!$this->form_validation->run())
		{
			$this->session->set_flashdata('errors', $this->form_validation->_error_array);
			$this->session->set_flashdata('refill', $_POST);

			redirect('/signup');
		} else {
			$this->load->library(array('encrypt'));
		
			$data = array(
				'username' 		=> $this->input->post('username'),
				'email' 		=> $this->input->post('email'),
				'password' 		=> $this->encrypt->salt_pass($this->input->post('password')),
				'last_ip'		=> $_SERVER['REMOTE_ADDR'],
				'created'		=> '0000-00-00',
				'last_login'	=> '0000-00-00',
				'modified'		=> '0000-00-00'
			);
			if($this->User_model->create_user($data)>0)
			{
				$data = array(
					'username'  => $user->username,
					'email'     => $user->email,
					'logged_in' => TRUE
				);
				
				$this->session->set_userdata($data);
				
				redirect('/');
				
			} else {
				echo 'DIDNT CREATE';
			}			
		}
	}
	
	/**
	 * Validates the username
	 *
	 * @access	public
	 * @returns	boolean
	 */
	function val_username($string)
	{
		if($this->User_model->is_username_available($this->input->post('username')))
			return TRUE;
		$this->form_validation->set_message('val_username','Username is taken.');
		return FALSE;
	}
	
	/**
	 * Validates the email
	 *
	 * @access	public
	 * @returns	boolean
	 */
	function val_email($string)
	{
		if($this->User_model->is_email_available($this->input->post('email')))
			return TRUE;
		$this->form_validation->set_message('val_email','This email has already been registered.');
		return FALSE;
	}
	
	/**
	 * Validates a reCatpcha
	 *
	 * @access	public
	 * @returns	boolean
	 */
	function val_recaptcha($string)
	{
		$resp = recaptcha_check_answer($this->config->item('recap_private'),
										$_SERVER['REMOTE_ADDR'],
										$this->input->post('recaptcha_challenge_field'),
										$this->input->post('recaptcha_response_field'));
		if(!$resp->is_valid)
		{
			$this->form_validation->set_message('val_recaptcha','Your answer for the security question was incorrect, please try again.');
			return FALSE;
		} else {
			return TRUE;
		}
	}
	
}
?>